package com.lujin.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lujin.commonutils.R;
import com.lujin.staservice.client.UcenterClient;
import com.lujin.staservice.entity.StatisticsDaily;
import com.lujin.staservice.mapper.StatisticsDailyMapper;
import com.lujin.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author lujin
 * @since 2021-07-13
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    //统计某一天注册人数,生成统计数据
    @Override
    public void registerCount(String day) {
        //添加数据之前，先删除表内相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("data_calculated",day);
        baseMapper.delete(wrapper);
        //远程调用统计某一天注册人数
        R registerR=ucenterClient.countRegister(day);
        Integer countRegister = (Integer)registerR.getData().get("countRegister");

        //把统计数据添加到数据库，统计分析表里面
        StatisticsDaily sta=new StatisticsDaily();
        sta.setRegisterNum(countRegister);//注册人数
        sta.setDateCalculated(day);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(sta);
    }

    //图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("data_calculated",begin,end);
        wrapper.select("data_calculated",type);
        List<StatisticsDaily> staList=baseMapper.selectList(wrapper);

        //因为返回的有两部分数据，日期 和日期对应的数量
        //后端的list集合会变成前端的json数组形式[1,2,3]，后端的map集合或者对象会变成json对象形式["name":"ll","age":20]
        //前端要求json数组结构，对应后端list集合
        //创建两个list集合，一个日期list，一个数量list
        List<String> data_calculatedList=new ArrayList<>();
        List<Integer> numDataList=new ArrayList<>();
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily=staList.get(i);
            //封装日期list集合
            data_calculatedList.add(daily.getDateCalculated());
            //封装对应数量list集合
            switch (type) {
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
            
        }
        //把封装之后的两个list集合放到map集合，进行返回
        Map<String, Object> map=new HashMap<>();
        map.put("data_calculatedList",data_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
